//
//  SMProgressView.m
//  SMCategory_Example
//
//  Created by yons on 19.10.20.
//  Copyright © 2020 865102930. All rights reserved.
//

#import "SMProgressView.h"

@interface SMProgressView ()

+ (SMProgressView *)shareProgress;

@property (nonatomic, strong) UIImageView *translateImgView; // 中间的图片
@property (nonatomic, strong) UILabel *msgLabel; // 提示文字

@end

@implementation SMProgressView

+ (SMProgressView *)shareProgress {
    static SMProgressView *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SMProgressView alloc] init];
        instance.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.25];
    });
    return instance;
}

- (instancetype)init {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        [self setUpUI];
    }
    return self;
}

#pragma mark -----------------------🍺🍺🍺----------------------- 初始化UI -----------------------🍺🍺🍺-----------------------

/// 初始化UI
- (void)setUpUI {
    [self addSubview:self.translateImgView];
    [self addSubview:self.msgLabel];
    self.translateImgView.center = self.center;
    self.msgLabel.frame = CGRectMake(CGRectGetMinX(self.translateImgView.frame) - 80, CGRectGetMaxY(self.translateImgView.frame) + 5, 240, 60);
}

#pragma mark -----------------------🍺🍺🍺----------------------- lazy -----------------------🍺🍺🍺-----------------------

- (UIImageView *)translateImgView {
    if (!_translateImgView) {
        _translateImgView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"loading" ofType:@"png"]]];
        _translateImgView.frame = CGRectMake(0, 0, 50, 50);
    }
    return _translateImgView;
}

- (UILabel *)msgLabel {
    if (!_msgLabel) {
        _msgLabel = [[UILabel alloc] init];
        _msgLabel.textAlignment = NSTextAlignmentCenter;
        _msgLabel.numberOfLines = 0;
    }
    return _msgLabel;
}

#pragma mark -----------------------🍺🍺🍺----------------------- func -----------------------🍺🍺🍺-----------------------

+ (void)startAnimation {
    SMProgressView *progressView = [SMProgressView shareProgress];
    [[UIApplication sharedApplication].delegate.window addSubview:progressView];
    [progressView start];
}

///  带提示文字
+ (void)startAnimationWithMsg:(NSString *)msg {
    SMProgressView *progressView = [SMProgressView shareProgress];
    progressView.msgLabel.text = msg;
    [[UIApplication sharedApplication].delegate.window addSubview:progressView];
    [progressView start];
}

+ (void)dismissAnimation {
    SMProgressView *progressView = [SMProgressView shareProgress];
    [progressView stop];
}

// 开始动画
- (void)start {
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    basicAnimation.toValue = @(-M_PI * 2);
    basicAnimation.repeatCount = INT_MAX;
    basicAnimation.duration = 1;
    [self.translateImgView.layer addAnimation:basicAnimation forKey:@"translate"];
}

- (void)stop {
    [self removeFromSuperview];
    [self.layer removeAnimationForKey:@"translate"];
}

#pragma mark -----------------------🍺🍺🍺----------------------- setter -----------------------🍺🍺🍺-----------------------

- (void)setImageNamed:(NSString *)imageNamed {
    _imageNamed = imageNamed;
    self.translateImgView.image = [UIImage imageNamed:imageNamed];
}

@end
